﻿using AntdUI;
using ImportPlcPointPosition.Models;
using ImportPlcPointPosition.Models.Table;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;

namespace ImportPlcPointPosition.Forms {
    public partial class TechniqueTemplateForm : BaseForm {
        /// <summary>
        /// MES数据库客户端
        /// </summary>
        private readonly SqlSugarClient _mesDBClient = SqlSugarHelper.MesDB;

        /// <summary>
        /// 工艺模板参数模型上下文
        /// </summary>
        private CksTechniqueTemplate _modelContext = null;

        public TechniqueTemplateForm(CksTechniqueTemplate context = null) {
            InitializeComponent();
            this._modelContext = context;
            LoadQuerySelect();
            this.Load += TechniqueTemplateForm_Load;
        }


        private void TechniqueTemplateForm_Load(object sender, EventArgs e) {
            if (_modelContext != null) {
                this.pageHeader1.Text = "编辑工艺模板参数";
                txtName.Text = $"{_modelContext.Name}";
                selPLC.SelectedValue = this.selPLC.Items.ToList<SelectItem>().Select(o => o.Tag as PlcConfig).FirstOrDefault(o => o.Id == _modelContext.PlcId);
                selReadDataList.SelectedValue = this.selReadDataList.Items.ToList<SelectItem>().Select(o => o.Tag as PlcBlockDb).FirstOrDefault(o => o.BlockDb == _modelContext.ReadDB);
                selWriteDataList.SelectedValue = this.selWriteDataList.Items.ToList<SelectItem>().Select(o => o.Tag as PlcBlockDb).FirstOrDefault(o => o.BlockDb == _modelContext.ReadDB);
                txtSendAction.Text = $"{_modelContext.SendActionId}";
                txtSyncAction.Text = $"{_modelContext.SyncActionId}";
                txtSeq.Text = $"{_modelContext.Sequence}";
            } else {
                this.pageHeader1.Text = "新建工艺模板参数";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e) {
            var item = _modelContext ?? new CksTechniqueTemplate();

            if (txtName.Text.IsNullOrEmpty()) {
                AntdUI.Message.error(this, "请填写模板名称！", autoClose: 2);
                return;
            }

            if (selPLC.SelectedValue == null) {
                AntdUI.Message.error(this, "请选择PLC！", autoClose: 2);
                return;
            }

            if (selReadDataList.SelectedValue == null) {
                AntdUI.Message.error(this, "请选择读DB！", autoClose: 2);
                return;
            }

            if (selWriteDataList.SelectedValue == null) {
                AntdUI.Message.error(this, "请选择写DB！", autoClose: 2);
                return;
            }

            item.Name = txtName.Text;
            item.PlcId = (selPLC.SelectedValue as PlcConfig).Id;
            item.Sequence = txtSeq.Text.IsNullOrEmpty() ? 99 : txtSeq.Text.ObjToInt();
            item.ReadDB = (selReadDataList.SelectedValue as PlcBlockDb).BlockDb;
            item.WriteDB = (selWriteDataList.SelectedValue as PlcBlockDb).BlockDb;
            item.SendActionId = txtSendAction.Text.ObjToInt();
            item.SyncActionId = txtSyncAction.Text.ObjToInt();
            item.Type = 1;
           

            if (item.Id == 0)
                SqlSugarHelper.MesDB.Insertable(item).ExecuteCommand();
            else
                SqlSugarHelper.MesDB.Updateable(item).ExecuteCommand();
            DialogResult = DialogResult.OK;
        }


        private void LoadQuerySelect() {
            //加载PLC列表
            this.selPLC.Items.Clear();
            var plcConfigList = _mesDBClient.Queryable<PlcConfig>().ToList().Select(o => new SelectItem($"{o.ProductionLineCode}-{o.WorkstationCode}({o.Remark})", o));
            this.selPLC.Items.AddRange(plcConfigList.ToArray());
            this.selPLC.SelectedIndexChanged += SelPLC_SelectedIndexChanged;
            this.selReadDataList.SelectedIndexChanged += SelReadDataList_SelectedIndexChanged;
            this.selWriteDataList.SelectedIndexChanged += SelWriteDataList_SelectedIndexChanged;
        }

        private void SelWriteDataList_SelectedIndexChanged(object sender, IntEventArgs e) {
            if (selWriteDataList.SelectedValue != null) {
                var sel = selWriteDataList.SelectedValue as PlcBlockDb;
                this.selWriteDataList.Text = $"{sel.BlockDb}({sel.WriteFromCellText.Text})";
            }
        }

        private void SelReadDataList_SelectedIndexChanged(object sender, IntEventArgs e) {
            if (selReadDataList.SelectedValue != null) {
                var sel = selReadDataList.SelectedValue as PlcBlockDb;
                this.selReadDataList.Text = $"{sel.BlockDb}({sel.WriteFromCellText.Text})";
            }
        }

        private void SelPLC_SelectedIndexChanged(object sender, IntEventArgs e) {
            this.selReadDataList.SelectedIndex = -1;
            this.selReadDataList.Items.Clear();
            this.selWriteDataList.SelectedIndex = -1;
            this.selWriteDataList.Items.Clear();
            this.txtName.Text = string.Empty;
            if (selPLC.SelectedValue != null) {
                //设置名称
                var sel = selPLC.SelectedValue as PlcConfig;
                selPLC.Text = $"{sel.ProductionLineCode}-{sel.WorkstationCode}({sel.Remark})";
                this.txtName.Text = $"{selPLC.Text}工艺";
                //加载PLC对应的DB列表
                var plcBlockDbList = _mesDBClient.Queryable<PlcBlockDb>()
                                    .WhereIF(selPLC.SelectedValue != null, o => o.PlcId == (selPLC.SelectedValue as PlcConfig).Id)
                                    .ToList().Select(o => new SelectItem($"{o.BlockDb}({o.WriteFromCellText.Text})", o));
                //加载读写DB列表
                this.selReadDataList.Items.AddRange(plcBlockDbList.ToArray());
               
                //加载写入DB列表
                this.selWriteDataList.Items.AddRange(plcBlockDbList.ToArray());
            }
        }
    }
}
